import { useEffect, useState } from 'react';
import { throttle} from 'lodash';


export default function useScrollPosition() {
  // 记录位置
  const [scrollX, setScrollX] = useState(0);
  const [scrollY, setScrollY] = useState(0);

  useEffect(() => {
    const handleScroll = throttle(
      function () {
        setScrollX(window.scrollX);
        setScrollY(window.scrollY);
      },
      100,
      [],
    );

    window.addEventListener('scroll', handleScroll);

    return () => {
      window.removeEventListener('scroll', handleScroll);
    };
  }, [scrollX,scrollY]);

  return { scrollX, scrollY };
}
